﻿using System;
using System.Collections.Generic;
using ICSharpCode.AvalonEdit.Rendering;
using System.Windows.Media;
using JmShared;
using System.Windows;
using System.Linq;
using JiveMessenger.Utilities;
using JiveMessenger.Models;

namespace JiveMessenger.AvalonEdit
{
    class LineColorizer : DocumentColorizingTransformer
    {

        private Brush Color { get; set; }
        private Typeface TypeFace { get; set; }
        public Dictionary<int, IContact> ClientLines { private get; set; }

        public LineColorizer(IContact user, Dictionary<int, IContact> clientLines)
        {
            if (user == null)
                throw new ArgumentOutOfRangeException("user", null, "User cannot be null.");
            this.ClientLines = clientLines;
            this.UserInfo = user;
        }
        private IContact _user { get; set; }
        public IContact UserInfo
        {
            get { return this._user; }
            set
            {
                this._user = value;
                this.Color = value.FontColor;
                this.TypeFace = new Typeface(value.FontFamily, value.FontStyle, value.FontWeight, FontStretches.Normal);
            }
        }

        protected override void ColorizeLine(ICSharpCode.AvalonEdit.Document.DocumentLine line)
        {
            IContact usr;
            if (this.ClientLines.TryGetValue(line.LineNumber,out usr))
            {
                if (!line.IsDeleted && usr != null && usr.Equals(this.UserInfo))
                {
                    ChangeLinePart(line.Offset, line.EndOffset, this.ApplyChanges);
                }
            }
        }

        void ApplyChanges(VisualLineElement element)
        {
            // apply changes here
            element.TextRunProperties.SetForegroundBrush(this.Color);


            element.TextRunProperties.SetTypeface(this.TypeFace);
        }
    }
}
